import {reqItemInfoBySkuId, reqAddOrUpdateCart} from "@/api"
const state = {
    detailInfo:{}
}
const mutations = {
    RECEIVE_GETDETAILINFO(state,data){
        state.detailInfo = data
    }
}
const actions = {
    //获取产品详情列表
    async getDetailInfo({commit},skuId){
        let resp = await reqItemInfoBySkuId(skuId)
        if(resp.code == 200){
            commit("RECEIVE_GETDETAILINFO", resp.data)
        }
    },
    //添加购物车
    async addOrUpdateShoppintCarts(context,{skuId,skuNum}){
        console.log("add or update shopping cart action enter.")
        let resp = await reqAddOrUpdateCart(skuId,skuNum)
        if(resp.code == 200){
            return 'ok.'
        } else {
            return Promise.reject(new Error("add or update shopping carts failed."))
        }
    }
}
const getters = {
    skuInfo(state){
        console.log(state)
        return state.detailInfo.skuInfo || {}
    },
    spuSaleAttrList(state){
        return state.detailInfo.spuSaleAttrList || []
    }
}

export default {
    state,
    mutations,
    actions,
    getters
}